package demo.activiti.workflow.web;

import com.google.common.collect.Lists;
import demo.activiti.common.Result;
import demo.activiti.common.web.BaseController;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * Created by wgc on 2017/4/8.
 */
@Controller
@RequestMapping(value = "${adminPath}/workflow")
public class WorkflowController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RepositoryService  repositoryService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView();
        List<ProcessDefinition> processDefinitionList = Lists.newArrayList();
        try {
            processDefinitionList = repositoryService.createProcessDefinitionQuery()
                    .latestVersion().list();
        } catch (Exception e) {
            logger.error("查询流程定义时发生错误：{}", e.getMessage());
        }
        mv.setViewName("workflow/index");
        mv.addObject("pdList", processDefinitionList);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        try {
            List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
                    .latestVersion().list();
            result.setData(processDefinitionList);
        } catch (Exception e) {
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping(value = "/deploy", method = RequestMethod.GET)
    public String deployGet(HttpServletRequest request, HttpServletResponse response) {
        return "workflow/deploy";
    }

    @ResponseBody
    @RequestMapping(value = "/deploy", method = RequestMethod.POST)
    public Object deployPost(HttpServletRequest request, HttpServletResponse response, @RequestParam("file")MultipartFile file) {
        logger.info("========== deploy ==========");

        Result result = new Result();
        try {
            if (file.isEmpty()) {
               throw new Exception("Please select a file to upload.");
            }
            String resourceName = file.getOriginalFilename();
            InputStream in = file.getInputStream();
            ZipInputStream zip = new ZipInputStream(in);
            repositoryService.createDeployment().addZipInputStream(zip).deploy();
        } catch (Exception e) {
            result.setCode(500);
            logger.error("部署流程定义时发生错误：{}", e.getMessage());
        }

        return new Result();
    }
}
